package com.android.nearshop.app.activity.map;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.MyApplication;
import com.android.nearshop.app.widget.RefreshView;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.model.Addr;
import com.android.nearshop.util.Constant;
import com.android.nearshop.util.LocationUtils;
import com.android.nearshop.util.Util;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-8
 * @see : 商家位置地图视图,包括线路起始点的选择 
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class ShopLocationActivity extends MainMapActivity 
implements RouteSearDialog.DelayLocationListener
{
    private RouteSearDialog mRouteSearDialog;
    private View mPopView = null;
    private OverItemT mShopOvlay;
    private OverItemT mMyLocOvlay;
    protected String mShopName;
    protected GeoPoint mShopPoint;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        if (!handIntent())
        {
            finish();
            return;
        }
        super.onCreate(arg0);
        setContentView(R.layout.activity_map);              
        reLocation(false);
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        showShopLocation();
    }
    
    @Override
    protected void onPause()
    {
        
        super.onPause();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(R.string.title_shopmap);
        titleBar.setLeftBg(R.drawable.back_btn_background);
        titleBar.setLeftImage(0);
        titleBar.setRightVisible(false);
        final RefreshView refreshView = new RefreshView(this, null);
        titleBar.setRigthBg(0);
        titleBar.setCustomRightView(refreshView);
        refreshView.getButton().setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                refreshView.reFresh();
            }
        });
        
        
    }
    
    private boolean handIntent()
    {
        Addr shopAddr = (Addr) getIntent().getSerializableExtra(Constant.TAG_ADDR);
        if (shopAddr == null)
        {
            return false;
        }
        this.mShopName = shopAddr.address;
        this.mShopPoint = shopAddr.toGeoPoint();
        return true;
    }

    private void showPopView(GeoPoint paramGeoPoint)
    {
        if (this.mPopView.isShown())
        {
            return;
        } else
        {
            MapView.LayoutParams localLayoutParams = new MapView.LayoutParams(
                    -2, -2, paramGeoPoint, 81);
            this.mMapView.updateViewLayout(this.mPopView, localLayoutParams);
            this.mPopView.setVisibility(View.VISIBLE);
        }
    }

    private void showRouteSearchLayout()
    {
        if(mRouteSearDialog != null)
        {
            if(!mRouteSearDialog.isShowing())
            {
                mRouteSearDialog.show();
            }
        }else
        {
            mRouteSearDialog = RouteSearDialog.createRouteSearDialog(this, this, getAddrInfo(2));
            mRouteSearDialog.show();
        }
    }
    
    private void showShopLocation()
    {
        try
        {
            boolean isRealMyloc = !Constant.mIsFixLocation && MyApplication.getContext().getLocMode() == Constant.LOC_CUR;
            if(mShopOvlay == null)
            {
                Drawable localDrawable = getResources().getDrawable(R.drawable.shopmark);
                localDrawable.setBounds(0, 0, localDrawable.getIntrinsicWidth(), localDrawable.getIntrinsicHeight());
                mShopOvlay = new OverItemT(localDrawable, this,mShopPoint);
                this.mMapView.getOverlays().add(mShopOvlay);
            }           
            if(mPopView == null)
            {
                this.mPopView = super.getLayoutInflater().inflate(R.layout.popview_shop_map_location, null);
                ((TextView) this.mPopView.findViewById(R.id.title)).setText(this.mShopName);
                this.mMapView.addView(this.mPopView, new MapView.LayoutParams(-2,-2, this.mShopPoint, 81));
                this.mPopView.setVisibility(View.VISIBLE);
                ((ImageView) this.mPopView.findViewById(R.id.search_route))
                        .setOnClickListener(new View.OnClickListener()
                        {
                            public void onClick(View paramView)
                            {
                                showRouteSearchLayout();
                            }
                        });
            }
            // 当前位置
            GeoPoint mylocation = null;
            if(MyApplication.getContext().getLocMode() == Constant.LOC_CITY)
            {
                // 城镇坐标需要校准
                mylocation = LocationUtils.checkLocation(Util.locationToGeoPoint(mMyLocation));
            }else
            {
                // 地图与当前定位就不需要校准
                mylocation = Util.locationToGeoPoint(mMyLocation);
            }
            if(isRealMyloc)
            {
                showMyPosition(true,false);
            }else
            if(mMyLocOvlay == null)
            {
                Drawable localDrawable = getResources().getDrawable(R.drawable.poi);
                localDrawable.setBounds(0, 0, localDrawable.getIntrinsicWidth(), localDrawable.getIntrinsicHeight());
                mMyLocOvlay = new OverItemT(localDrawable, this, mylocation);
                this.mMapView.getOverlays().add(mMyLocOvlay);
                mMyLocOvlay.setonTapAble(false);
            }
            if(mMyLocation != null)
            {
                List<GeoPoint> points = new ArrayList<GeoPoint>(2);
                points.add(mShopPoint);
                points.add(mylocation);
                LocationUtils.centerPoints(mMapView.getController(), points);
            }else
            {
                mMapController.setCenter(mShopPoint);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    class OverItemT extends ItemizedOverlay<OverlayItem>
    {
        private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
        private boolean onTapable = true;
        private Drawable marker;

        public OverItemT(Drawable marker, Context arg3, GeoPoint point)
        {
            super(boundCenterBottom(marker));
            this.marker = marker;
            this.mGeoList.add(new OverlayItem(point, "", ""));
            populate();
        }

        public void setonTapAble(boolean onTapable)
        {
            this.onTapable = onTapable;
        }
        
        protected OverlayItem createItem(int paramInt)
        {
            return (OverlayItem) this.mGeoList.get(paramInt);
        }

        public void draw(Canvas paramCanvas, MapView paramMapView,boolean paramBoolean)
        {                        
            Projection localProjection = paramMapView.getProjection();
            for (int i = size() - 1; i >= 0; i--)
            {
                OverlayItem localOverlayItem = getItem(i);
                String str = localOverlayItem.getTitle();
                Point localPoint = localProjection.toPixels(localOverlayItem.getPoint(), null);
                Paint localPaint = new Paint();
                localPaint.setColor(0xFF0000FF);
                localPaint.setTextSize(15.0f);
                paramCanvas.drawText(str, localPoint.x, localPoint.y,localPaint);
            }
            super.draw(paramCanvas, paramMapView, paramBoolean);
            boundCenterBottom(this.marker);
        }

        protected boolean onTap(int paramInt)
        {
            if(!onTapable)
            {
                return true;
            }
            setFocus((OverlayItem) this.mGeoList.get(paramInt));
            GeoPoint localGeoPoint = ((OverlayItem) this.mGeoList.get(paramInt)).getPoint();
            showPopView(localGeoPoint);
            return true;
        }

        public boolean onTap(GeoPoint paramGeoPoint, MapView paramMapView)
        {
            mPopView.setVisibility(View.GONE);
            return super.onTap(paramGeoPoint, paramMapView);
        }

        public int size()
        {
            return this.mGeoList.size();
        }
    }

    @Override
    public GeoPoint getGeoPoint(int type)
    {
       if(type == 0)
       {
           if(mMyLocation != null)
           {
               GeoPoint mylocation = null;
               if(MyApplication.getContext().getLocMode() == Constant.LOC_CITY)
               {
                   // 城镇坐标需要校准
                   mylocation = LocationUtils.checkLocation(Util.locationToGeoPoint(mMyLocation));
               }else
               {
                   // 地图与当前定位就不需要校准
                   mylocation = Util.locationToGeoPoint(mMyLocation);
               }
               return mylocation;
           }else
           {
               return null;
           }
       }else
       {
           return mShopPoint;
       }
    }

    @Override
    public String getAddrInfo(int type)
    {
        if(type == 0)
        {
            if(mMyAddress != null)
            {
                return mMyAddress.addressComponents.city;
            }else
            {
                return null;
            }
        }else
        if(type == 1)
        {
            return mShopName;
        }else
        {
            if(mMyAddress != null)
            {
                return mMyAddress.addressComponents.district + 
                (mMyAddress.addressComponents.street == null ? "": mMyAddress.addressComponents.street)
              + (mMyAddress.addressComponents.streetNumber == null ? "": mMyAddress.addressComponents.streetNumber);
            }else
            {
                return null;
            }
        }
    }
    
    @Override
    public boolean isLocating()
    {
        return mMyLocation == null && mMapManager.isLocating();
    }
    
    public static boolean startActivity(Context context, Addr param)
    {
        if(param == null || !param.isValid()) return false;
        context.startActivity(getStartActivity(context, param));
        return true;
    }
    
    public static Intent getStartActivity(Context context, Addr param)
    {
        if(param == null || !param.isValid()) return null;
        Intent it = new Intent(context, ShopLocationActivity.class);
        it.putExtra(Constant.TAG_ADDR, param);
        return it;
    }
}
